<?php

/**
 * @Author: zhibinm (113664000@qq.com)
 * @Date:   2018-04-18 14:12:44 
 * @Copyright:   xuebingsi
 * @Last Modified by:   Zhibinm
 * @Last Modified time: 2018-04-20 09:18:20
 */
// var_dump(extension_loaded("GD"));
// 创建画布
$src = imageCreateTrueColor(400,200);
// $src = imageCreateFrompng('./test.png');
// 先颜色
$color = imageColorAllocate($src,255,255,255); 
// $color = imageColorAllocate($src,mt_rand(150,255),mt_rand(150,255),mt_rand(150,255)); 
//填充
imageFill($src,0,0,$color);

// var_dump($src);

// 画空心

// for ($i=0; $i <10 ; $i++) { 

// 	// 先颜色
// 	$color = imageColorAllocate($src,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255)); 

// 	imagerectangle ($src, mt_rand(0,400) , mt_rand(0,200) , mt_rand(0,400) , mt_rand(0,200) , $color);
// }
$color = imageColorAllocate($src,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255)); 

// 画实习
// imagefilledrectangle($src, 10 , 10 , 200 , 100 , $color);

$color = imageColorAllocate($src,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255)); 


// imageellipse ( $src, 200 , 100 , 50 ,100 , $color);

// imagefilledellipse  ( $src, 200 , 100 , 50 ,100 , $color);
// 

// imageline ($src , 0 , 0 , 200 ,100 ,$color);
// for ($i=0; $i <100 ; $i++) { 
// 	$color = imageColorAllocate($src,mt_rand(200,255),mt_rand(200,255),mt_rand(200,255)); 
// 	// imagesetpixel ($src , mt_rand(0,400) , mt_rand(0,200),  $color);
	
// 	$dx = mt_rand(0,400);

// 	$dy = mt_rand(0,200);

// 	imagefilledrectangle($src, $dx-3 , $dy-3 , $dx+3 ,$dy+3 , $color);
// }
// 

$width = 400;
$height = 200;
$lenght = 4;
$fontsize = 80; 

$str = "qwertyui9o0pasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890";

for ($i=0; $i < 100; $i++) { 

	$color = imageColorAllocate($src,mt_rand(150,255),mt_rand(150,255),mt_rand(150,255));

	$word = $str[mt_rand(0,strlen($str)-1)];

	imagettftext ($src ,15,mt_rand(-15,15), mt_rand(0,400) ,mt_rand(0,200), $color ,"./Sandbox Melodrama.ttf", $word);

}

$data = '';

for ($i=0; $i < $lenght; $i++) { 
	$color = imageColorAllocate($src,mt_rand(0,150),mt_rand(0,150),mt_rand(0,150)); 

	$word = $str[mt_rand(0,strlen($str)-1)];

	$data .= $word;

	$x = (($width/$lenght)-$fontsize*0.6)/2+($width/$lenght)*$i;

	$y = ($height +$fontsize)/2;

	imagettftext ($src ,$fontsize,mt_rand(-15,15), $x ,$y, $color ,"./Sandbox Melodrama.ttf", $word);
}

session_start();

$_SESSION['code'] = $data;

// imagettftext ($src ,100, 0, 20 ,120 , $color ,"./Sandbox Melodrama.ttf", "d");
// imagettftext ($src ,100, 0, 120 ,120 , $color ,"./Sandbox Melodrama.ttf", "f");
// imagettftext ($src ,100, 0, 220 ,120 , $color ,"./Sandbox Melodrama.ttf", "g");
// imagettftext ($src ,100, 0, 320 ,120 , $color ,"./Sandbox Melodrama.ttf", "h");
// imagettftext ($src ,100, 30 , 100 ,100 , $color ,"./Sandbox Melodrama.ttf", "f");

// 文字左下角

// imagettftext ($src ,80, mt_rand(-15,15) ,0 ,0 , $color ,"./Tensentype-MianBaoF.ttf", "学");

header("Content-type: image/png");
//输出画布
imagepng($src);